package com.itheima.contorller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.ShoppingCart;
import com.itheima.domain.User;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    //购物车页面数据
    @GetMapping("/list")
    public R<List<ShoppingCart>> listt(ShoppingCart shoppingCart){

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(list);
    }
    //清空购物车
    @DeleteMapping("/clean")
    public R<String> delete(ShoppingCart shoppingCart){

        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> soppingCartambdaQueryWrapper = new LambdaQueryWrapper<>();
        soppingCartambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
        List<ShoppingCart> list = shoppingCartService.list(soppingCartambdaQueryWrapper);
        for (ShoppingCart cart : list) {
            Long userId = cart.getUserId();
            shoppingCartService.removeById(userId);
        }
        return R.success("清空成功");
    }

    //添加到购物车
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();

        shoppingCart.setUserId(currentId);
        //查询购物车里面是否有重复的套餐
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> soppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        soppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
        if (dishId!=null){
           soppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            soppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(soppingCartLambdaQueryWrapper);
        if (one==null){
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;

        }else {
            Integer number = one.getNumber();
            one.setNumber(number+1);
            shoppingCartService.updateById(one);
        }
       return R.success(one);
    }
    //减少订单功能
    @PostMapping("/sub")
    public R<ShoppingCart> decreasesave( @RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> shoppingCartambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
       if (shoppingCart.getDishId()==null){
           shoppingCartambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
           ShoppingCart one = shoppingCartService.getOne(shoppingCartambdaQueryWrapper);
           Integer number = one.getNumber();
           one.setNumber(number-1);
           shoppingCartService.updateById(one);
           return R.success(one);
       }else {
           shoppingCartambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
           ShoppingCart one = shoppingCartService.getOne(shoppingCartambdaQueryWrapper);
           Integer number = one.getNumber();
           one.setNumber(number-1);
           shoppingCartService.updateById(one);
           return R.success(one);
       }

    }

}
